var cssOptWhenEnd = {
	config:null,
	loadConfig:function(){
		var url = getRootPath();
		var yuming = getYuming(url);
		if(adClearer.config){
			cssOptWhenEnd.config = adClearer.config;
			cssOptWhenEnd.dealConfig();
		}else{
			try{
				chrome.storage.sync.get(yuming, function(res) {
					res = res[yuming];
					cssOptWhenEnd.config = res;
					cssOptWhenEnd.dealConfig();
				});
			}catch(e){
				console.info(e)
			}
		}
		
	},
	dealConfig(){
		var res = cssOptWhenEnd.config;
		if(res){
			if(res.status != "on")return;
			var cssJson = res.style || "{}";
			cssJson = JSON.parse(cssJson);
			cssOptWhenEnd.addStyleFromConfig(document,[cssJson]);
			cssOptWhenEnd.attrReplace(res.attrReplace);
			cssOptWhenEnd.addHideButton(res.hide);
		}
	},
	addStyleFromConfig:function(pDocument,styleConfigList){
		if(!pDocument || !styleConfigList || styleConfigList.length == 0 || !styleConfigList[0]){
			return ;
		}
		for(var i=0; i < styleConfigList.length; i++){
			var styleConfig = styleConfigList[i];
			var iframeConfig = styleConfig.iframe;
			var key = styleConfig.key;
			var currConfig = styleConfig.style;
			if(!key){
				if(currConfig)adClearer.addCSS(pDocument,currConfig);
				if(iframeConfig && iframeConfig.length > 0){
					cssOptWhenEnd.addStyleFromConfig(pDocument,iframeConfig);
				}
			}else{
				var iframeDoms = pDocument.querySelectorAll(key);
				if(iframeDoms && iframeDoms.length > 0){
					for(var k=0; k < iframeDoms.length; k ++){
						var iframeDocument = iframeDoms[k].contentDocument;
						if(iframeDocument){
							adClearer.addCSS(iframeDocument,currConfig);
						}
						if(iframeConfig && iframeConfig.length > 0){
							cssOptWhenEnd.addStyleFromConfig(iframeDocument,iframeConfig);
						}
					}
				}
			}
			
		}
		
	},
	// [{"key":"[data-original]","attr":"src=:data-original"}]
	attrReplace:function(attrConfig){
		if(!attrConfig)return;
		attrConfig = JSON.parse(attrConfig);
		if(!attrConfig)return;
		for(var i=0; i <attrConfig.length; i++){
			var item = attrConfig[i];
			var key = item.key;
			var $keyObj = $(key);
			if(!$keyObj || $keyObj.length < 1)continue;
			var attr2 =  (item.attr || "").split("=");
			if(!attr2 || attr2.length < 1)continue;
			if(attr2.length == 1)attr2.push("");
			var attr = attr2[1];
			if(attr.charAt(0) == ':'){
				var fromAttr = attr.substring(1);
				for(var j=0; j < $keyObj.length; j++){
					$tem = $keyObj.eq(j);
					$tem.attr(attr2[0],$tem.attr(fromAttr));
				}
			}else if(attr.charAt(0) == '@'){
				var t = attr.substring(1).split("?");
				if(!t || t.length != 2)continue;
				var $tar = $(t[0]);
				if(!$tar || $tar.length < 1)continue;
				for(var j=0; j < $keyObj.length; j++){
					$tem = $keyObj.eq(j);
					$tem.attr(attr2[0],$tar.eq(0).attr(t[1]));
				}
			}else{
				for(var j=0; j < $keyObj.length; j++){
					$tem = $keyObj.eq(j);
					$tem.attr(attr2[0],attr2[1]);
				}
			}
		}
	},
	
	// [{"key":"","name":"隐藏条件"}]
	addHideButton:function(btnConfig){
		if(!btnConfig)return;
		btnConfig = JSON.parse(btnConfig);
		if(!btnConfig)return;
		cssOptWhenEnd.config.hide = btnConfig;
		var hideHtml = "";
		for(var i=0; i < btnConfig.length; i++){
			var item = btnConfig[i];
			if(!item.key)continue;
			var styleStr = (" style='" + (item.style || "" ) + "' ");
			hideHtml += "<button type='button' class='layui-btn layui-btn-primary' ind='"+i+"' "+styleStr+">"
			+item.name+"</button>";
			if(localStorage.getItem("hide-key-"+item.key) == "1"){
				var $target = $(item.key);
				if(!$target || $target.length < 1)continue;
				$target.addClass("clear-hideitem");
			}
		}
		if(hideHtml){
			var $exit = $("#clearbtndiv");
			if($exit || $exit.length > 0)$exit.remove();
			var $eplanddiv = $("#eplanddiv");
			if($eplanddiv.length > 0){
				hideHtml= "<div id='clearbtndiv'>" + hideHtml + "</div>";
				$eplanddiv.append(hideHtml);
			}else{
				hideHtml= "<div id='eplanddiv' style='position:fixed;top:30px;right:30px;z-index: 999999;'><div id='clearbtndiv'>" + hideHtml + "</div></div>";
				$("html").append(hideHtml);
			}
			
			$("#clearbtndiv").off().click(function(e){
				cssOptWhenEnd.hideOrShow($(e.target).attr("ind"));
			})
		}
	},
	hideOrShow:function(ind){
		
		var config = cssOptWhenEnd.config.hide ;
		var item = config[ind-0];
		if(!item)return;
		var $target = $(item.key);
		if(!$target || $target.length < 1)return;
		var f = "0";
		if($target.hasClass("clear-hideitem")){
			$target.removeClass("clear-hideitem");
			f="0";
		}else{
			$target.addClass("clear-hideitem");
			f="1";
		}
		localStorage.setItem("hide-key-"+item.key,f);
	}
	
}
cssOptWhenEnd.loadConfig();